<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class General_Model extends Model 
{
    function General_Model(){
        parent::Model();
    }
    
    function sendEmail($emails, $subject, $templateName, $data = array(), $mailType = 'html', $from = null){

	    //$configEmail = $this->config->item('email');
	    $configEmail['mailtype'] = $mailType;
	    $this->load->library('email', $configEmail);
	    $this->email->set_newline("\r\n");
	    $this->email->initialize($configEmail);
	    /**
	     * Load email template from database
	     */
	    $result = $this->db->get_where('cvj_template_mail', array('name'=> $templateName, 'enabled'=> 1));
	    $result = $result->result_array();
	    if (empty($result)){
	        return false;
	    }
	    $content = $result[0];
        extract($data);
        ob_start();
	    echo eval ('?>'.$content['content']);
	    $content = ob_get_contents();
	    @ob_end_clean();
	    /**
	     * Send mail
	     */
	    try {
            $this->email->clear();
            $this->email->to($emails);
            //$this->email->from($message['email'],$message['first_name']." ".$message['surname']);
            $this->email->from('test@bjornekaelderen.dk');
            $this->email->subject($subject);
            $this->email->message($content);
            $this->email->send();
	    }catch (Exception $e){
	        //Do nothing
	    }
	}
    function sendEmail2($emails, $subject, $templateName, $data = array(), $mailType = 'html', $from = null){

	    //$configEmail = $this->config->item('email');
	    $configEmail['mailtype'] = $mailType;
	    $this->load->library('email', $configEmail);
	    $this->email->set_newline("\r\n");
	    $this->email->initialize($configEmail);
	    /**
	     * Load email template from database
	     */
	    $result = $this->db->get_where('cvj_template_mail', array('name'=> $templateName, 'enabled'=> 1));
	    $result = $result->result_array();
	    if (empty($result)){
	        return false;
	    }
	    $content = $result[0];
        extract($data);
        ob_start();
	    echo eval ('?>'.$content['content']);
	    $content = ob_get_contents();
	    @ob_end_clean();
	    /**
	     * Send mail
	     */
	    try {
            $this->email->clear();
            $this->email->to($emails);
            //$this->email->from($message['email'],$message['name']);
            $this->email->from('test@bjornekaelderen.dk');
            $this->email->subject($subject);
            $this->email->message($content);
            $this->email->send();
	    }catch (Exception $e){
	        //Do nothing
	    }
	}
	// random code for Forgot password...
	function getRandomCode($length = 8) 
    {
        $chars      = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $activeCode = '';
        for ($i = 1; $i <= $length; $i++) {
            $activeCode .= $chars{rand(0, 61)};
        }
        return $activeCode;
    }

    function get_historie($name=null){
		$this->db->from('information_description');
    	$this->db->select('information_description.*');
    	$this->db->join('information', 'information_description.information_id = information.information_id');
    	$this->db->where('keyword', $name);
    	$result = $this->db->get();
	    return $result->result();
	}
    function get_faq($type=null){
        $this->db->from('faq_description');
    	$this->db->select('faq_description.*, faq.name');
    	$this->db->join('faq', 'faq_description.faq_id = faq.id');
    	$this->db->where('type', $type);
        $this->db->order_by('sorting', 'asc');
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_article($type=null){
        $this->db->from('article_description');
    	$this->db->select('article_description.*, article.name');
    	$this->db->join('article', 'article_description.article_id = article.id');
    	$this->db->where('type', $type);
        $this->db->order_by('sorting', 'asc');
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_article_description($id=null){
        $this->db->from('article_description');
    	$this->db->select('article_description.*');
    	$this->db->where('id', $id);
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_category($type=null){
        $this->db->from('category');
    	$this->db->select('category.*');
    	$this->db->where('type', $type);
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_category_id($id=null){
        $this->db->from('category');
    	$this->db->select('category.*');
    	$this->db->where('category_id', $id);
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_product($id=null){
        $this->db->from('product');
    	$this->db->select('product.*');
    	$this->db->where('category_id', $id);
        $this->db->order_by('sorting', 'asc');
    	$result = $this->db->get();
	    return $result->result();
    }
    function insert_order($message=null){
        $this->db->insert('orders', $message);
        return true;
    }
    function get_top_product_id(){
        $this->db->from('product');
        $this->db->select_max('product_id');
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_top_product($id=null){
        $this->db->from('product');
    	$this->db->select('product.*');
        $this->db->where('product_id', $id);
    	$result = $this->db->get();
	    return $result->result();
    }
    function insert_newsletter($message=null){
        $this->db->insert('newsletter', $message);
        return true;
    }
    function get_gallery(){
        $this->db->from('gallery');
    	$this->db->select('gallery.*');
        $this->db->order_by('id', 'desc');
    	$result = $this->db->get();
	    return $result->result();
    }
    function get_slide(){
        $this->db->from('slideshow');
    	$this->db->select('slideshow.*');
        $this->db->order_by('sorting', 'asc');
    	$result = $this->db->get();
	    return $result->result();
    }
}